package edu.wfu.preparedDishes.commodity.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.wfu.preparedDishes.commodity.entity.CartVo;
import edu.wfu.preparedDishes.commodity.entity.commodityVo;
import edu.wfu.preparedDishes.common.utils.PageUtils;
import edu.wfu.preparedDishes.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import edu.wfu.preparedDishes.commodity.entity.CartEntity;
import edu.wfu.preparedDishes.commodity.service.CartService;

import javax.servlet.http.HttpServletRequest;


/**
 * 购物车记录表;
 *
 * @author hero_miracle
 * @email hero_miracle@gmail.com
 * @date 2023-11-22 13:06:46
 */
@RestController
@RequestMapping("commodity/cart")
@Slf4j
public class CartController {
    @Autowired
    private CartService cartService;

    @RequestMapping("/num")
    public R num(HttpServletRequest request){
        Map<String,Object> res=new HashMap<>(0);
        String userId=(String) request.getAttribute("userId");
        try {
            res.put("num",cartService.getNum(userId));
            return R.ok(res);
        }catch (Exception e){
            log.error("",e);
            return R.error("系统繁忙");
        }
    }

    @RequestMapping("/sum")
    public R sum(HttpServletRequest request){
        Map<String,Object> res=new HashMap<>(0);
        String userId=(String) request.getAttribute("userId");
        try {
            res.put("sum",cartService.sum(userId));
            return R.ok(res);
        }catch (Exception e){
            log.error("",e);
            return R.error("系统繁忙");
        }


    }

    @RequestMapping("/carts")
    public List<CartVo> getCommodities(
            @RequestParam(name = "pageSize", defaultValue = "5") int pageSize,
            @RequestParam(name = "page", defaultValue = "0") int page,
            HttpServletRequest request) {
        String userId=(String) request.getAttribute("userId");
        int offset = page * pageSize;
        return cartService.queryList(pageSize, offset, userId);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = cartService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{recordId}")
    public R info(@PathVariable("recordId") String recordId){
		CartEntity cart = cartService.getById(recordId);
        return R.ok().put("cart", cart);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(HttpServletRequest request,
                  @RequestParam(name = "skuId") String skuId,
                  @RequestParam(name = "num") Integer num){
        String userId=(String) request.getAttribute("userId");
        CartEntity have = cartService.isHave(skuId,userId);
        if (have!=null){
            cartService.addMore(have.getRecordId(),num);
            return R.ok();
        }else {
            CartEntity cart=new CartEntity();
            cart.setNumber(num);
            cart.setUserId(userId);
            cart.setSkuId(skuId);
            cartService.save(cart);
            return R.ok();
        }

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CartEntity cart){
		cartService.updateById(cart);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] recordIds){
		cartService.removeByIds(Arrays.asList(recordIds));
        return R.ok();
    }

    @RequestMapping("/add")
    public R add(@RequestParam(name = "recordId", defaultValue = "") String recordId){
        cartService.add(recordId);
        return R.ok();
    }

    @RequestMapping("/reduce")
    public R reduce(@RequestParam(name = "recordId", defaultValue = "") String recordId){
        cartService.reduce(recordId);
        return R.ok();
    }

}
